package com.tdt.alarm.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import com.tdt.alarm.model.AlarmDBHelper;
import com.tdt.alarm.model.SetAlarm;
import com.tdt.alarm.model.Utils;

public class CheckAlarmService extends Service {

	Context context;
	boolean alarmSet = true;
	AlarmDBHelper alarmDB = new AlarmDBHelper(this);
	

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		// super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		AlarmDBHelper db = new AlarmDBHelper(getApplicationContext());
		// Toast.makeText(getApplication(), "test chay nen z",
		// Toast.LENGTH_SHORT).show();
		List<SetAlarm> listAlarm = new ArrayList<SetAlarm>();
		listAlarm = db.getAlarms();

		if (Utils.countActiveCurrent(listAlarm) > 0) {
			CheckRepeate();
			if (alarmSet) {
				setOneShowAlarm(null, Utils.getAlarmActiveFirst(listAlarm),
						listAlarm);
			}
		}

		return START_STICKY;
	}

	public void setOneShowAlarm(Calendar alarmPoint, int position,
			List<SetAlarm> list) {
		context = getApplicationContext();
		Intent intent = new Intent(this, OneShotAlarm.class);
		PendingIntent sender = PendingIntent
				.getBroadcast(context, 0, intent, 0);
		SetAlarm alarm = Utils.getCurrentAlarm(position, list);
		// Toast.makeText(
		// this,
		// "next alarm chay o day: " + alarm.getTime().getHour() + ":"
		// + alarm.getTime().getMinute(), Toast.LENGTH_SHORT)
		// .show();
		// alarmPoint = Calendar.getInstance();
		// alarmPoint.set(Calendar.HOUR_OF_DAY, alarm.getTime().getHour());
		// alarmPoint.set(Calendar.MINUTE, alarm.getTime().getMinute());
		Calendar calendar = Calendar.getInstance();

		int alarmHour = alarm.getTime().getHour();
		int alarmMinute = alarm.getTime().getMinute();

		int distance = alarmHour * 3600 + alarmMinute * 60
				- calendar.get(Calendar.HOUR_OF_DAY) * 3600
				- calendar.get(Calendar.MINUTE) * 60
				- calendar.get(Calendar.SECOND);

		// int distance = alarmPoint.get(Calendar.HOUR_OF_DAY) * 3600
		// + alarmPoint.get(Calendar.MINUTE) * 60
		// - calendar.get(Calendar.HOUR_OF_DAY) * 3600
		// - calendar.get(Calendar.MINUTE) * 60
		// - calendar.get(Calendar.SECOND);
		if (distance < 0)
			distance += 86400;

		calendar.add(Calendar.SECOND, distance);

		// Schedule the alarm!
		AlarmManager am = (AlarmManager) this
				.getSystemService(Context.ALARM_SERVICE);
		am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

		// mToast = Toast.makeText(this, "Next alarm in: " + distance + "s",
		// Toast.LENGTH_SHORT);
		// mToast.show();
	}

	public void CheckRepeate() {
		List<SetAlarm> list = alarmDB.getAlarms();
		SetAlarm alarm = Utils.getAlarmRingFirst(list);
		
		boolean[] repeatArr = alarm.getRepeatingDays();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, alarm.getTime().getHour());
		calendar.set(Calendar.MINUTE, alarm.getTime().getMinute());
		calendar.set(Calendar.SECOND, 00);

		final int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		final int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		final int nowMinute = Calendar.getInstance().get(Calendar.MINUTE);
		Toast.makeText(getApplicationContext(), alarm.getTime().toString()+alarm.getRepeatingDay(0)+""+nowDay, Toast.LENGTH_LONG).show();
		if (alarm.getRepeatingDay(nowDay-1)) {
			alarmSet = true;
		} else
			alarmSet = false;
		
		
	}

}
